package com.jiyun.yonghu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jiyun.vo.Result;
import com.jiyun.yonghu.domain.Permissions;
import com.jiyun.yonghu.domain.PermissionsVo;
import com.jiyun.yonghu.service.PermissionsService;
import com.jiyun.yonghu.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleConteroller {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionsService permissionsService;
    @RequestMapping("list")
    public Result list(){
       return Result.seccuss(roleService.list());
    }
    @RequestMapping("listt")
    public Result listt(@RequestBody Permissions menu){

        List<PermissionsVo> deptVoList = findByPid(permissionsService.list(), -1);
        return Result.seccuss(deptVoList);
    }

    private List<PermissionsVo> findByPid(List<Permissions> list, Integer pid) {
        ArrayList<PermissionsVo> deptVos = new ArrayList<>();

        for (Permissions menu : list) {
            if (menu.getPid().equals(pid)){
                PermissionsVo menuVo = new PermissionsVo();
                BeanUtils.copyProperties(menu,menuVo);
                List<PermissionsVo> vos = findByPid(permissionsService.list(), menu.getId());
                if (CollectionUtils.isNotEmpty(vos)){
                    menuVo.setChildren(vos);
                }
                deptVos.add(menuVo);
            }

        }
        return deptVos;
    }
}
